/**
 * zmn文章模块-文章编辑
 * 文件命名格式：zmn.模块.表/功能.jas
 * @author duanzuocai
 * @since 2018/10/18
 */
layui.define(['form', 'layedit', 'laydate', 'upload','element'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let layedit = layui.layedit;
    let laydate = layui.laydate;
    let upload = layui.upload;
    let element = layui.element;

    form.render(null, "zmn-company-form");

    let company = json.company;
    if(company.type==130){
        $("input,textarea,select").prop("disabled", 'disabled').addClass("layui-disabled");
        $("#subBtn").hide();
    }
    form.render('select');

    // 自定义验证规则
    form.verify({
        title: function (value) {
            if (value.length < 2) {
                return "不能少于2个字符";
            }
            ;
        },
        numAndAZ: function (value) {
            if (value == null || value == "") {
                return;
            }
            if (!new RegExp(/^[0-9A-Za-z]+$/).test(value)) {
                return "只能是数字和字母";
            }
        },
        mobile: function (value) {
            if (value == null || value == "") {
                return;
            }
            if (!new RegExp(/^[0-9]{11}$/).test(value)) {
                return "手机号只能是11位数字";
            }
        },
        idCard: function (value) {
            if (value == null || value == "") {
                return;
            }
            if (!new RegExp(/^[0-9]{17}[0-9X]{1}$/).test(value)) {
                return "身份证号只能是18位，且数字或者X结尾";
            }
        },
        num: function (value) {
            if (value == null || value == "") {
                return;
            }
            if (!new RegExp(/^[0-9]+$/).test(value)) {
                return "只能是数字";
            }
        },
        minSort: function(value, item){ //value：表单的值、item：表单的DOM对象
            if (value < 5000){
                return "排序号不能小于5000";
            }
        }
    });
    //取消按钮
    $("button.cancleBtn").click(function () {
        parent.layui.admin.events.closeThisTabs();
    });

    // 提交
    form.on('submit(zmn-company-form-submit)', function (obj) {
        var data = obj.field;
        var url = "/company/addEdit.action";
        var openId = layer.open({type: 3});

        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify($.extend(json.company,data)),
            success: function (json) {
                layer.close(openId);
                layer.msg(json.message);
                if (json.status == 200) {
                    setTimeout(function () {
                        $("button.cancleBtn").click();
                    },1000)
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            }
        })
        return false;
    });

    // 分类下拉框联动
    zmnPage.laySelectLinkage(form, "/common/area/listChilds.action", "provinceId", "cityId", "districtId");

    /**
     *  图片上传
     */
    upload.render({
        elem: '#idcardImgSrc'
        , url: '/common/company/uploadLegalPersonImg'
        , size: 1024 * 5
        , accept: "images"
        , acceptMime: 'image/*'
        , exts: "jpg|jpeg|png"
        , data: {
            legalPersonIdcard: function () {
                return $('#legalPersonIdcard').val();
            }
        }
        , done: function (data) {
            if (data.status == 200) {
                $("#idcardImgSrc").attr("style", "background:url( " + data.message + "?" + Math.random()
                    + ");background-size:cover;width:100%;background-repeat:no-repeat;");
                $("#legalPersonImgsrc").val(data.message);
                $("#legalPersonImgsrcLink").attr("href", data.message);
                layer.msg("上传成功");
            } else {
                layer.msg("上传失败：" + data.message);
            }
        }
    });

    //对外暴露的接口
    exports('art_company_finance', {});
});